import time

# 全局变量
global_var = 1000000

# 未优化的代码
def unoptimized_function():
    start_time = time.time()
    result = 0
    for i in range(global_var):
        result += i
    end_time = time.time()
    print(f"Unoptimized function took {end_time - start_time:.6f} seconds")
    return result

# 优化后的代码
def optimized_function():
    start_time = time.time()
    global global_var
    local_var = global_var  # 将全局变量转换为局部变量
    result = 0
    for i in range(local_var):
        result += i
    end_time = time.time()
    print(f"Optimized function took {end_time - start_time:.6f} seconds")
    return result

if __name__ == "__main__":
    unoptimized_function()
    optimized_function()